var util = require("../../utils/util.js");
var app = getApp();
Page({
  data: {
    intheaters: {},
    comingsoon: {},
    top250: {},
    searchresult: {},
    containershow: true,
    searchpanelshow: false

  },
  onLoad: function (options) {

    var globalData = app.globalData;
    // 生命周期函数--监听页面加载
    var intheatersurl = globalData.g_doubanapi + "/v2/movie/in_theaters" + "?start=2&count=3";
    var comingsoonurl = globalData.g_doubanapi + "/v2/movie/coming_soon" + "?start=0&count=3";
    var top250url = globalData.g_doubanapi + "/v2/movie/top250" + "?start=0&count=3";
    this.getMovielist(intheatersurl, "intheaters", "正在热映");
    this.getMovielist(comingsoonurl, "comingsoon", "即将上映");
    this.getMovielist(top250url, "top250", "电影Top250");
  },
  getMovielist: function (url, movietype, cate) {
    var that = this;
    wx.request({
      url: url,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "content-type": "application/xml",
      }, // 设置请求的 header
      success: function (res) {
        // success

        that.progressData(res.data.subjects, movietype, cate);
      },
      fail: function (res) {
        // fail

      },
      complete: function (res) {
        // complete
      }
    })
  },
  progressData: function (data, movietype, cate) {
    var movie = [];
    for (var i in data) {
      var title = data[i].title;
      if (title.length > 6) {
        title = title.substring(0, 6) + ".";
      }
      var stars = util.toArray(data[i].rating.stars);
      var tpldata = {
        title: title,
        average: data[i].rating.average,
        coverageurl: data[i].images.large,
        movieid: data[i].id,
        stars: stars

      };
      movie.push(tpldata);
    }
    var lastData = {};
    lastData[movietype] = {
      movies: movie,
      cate: cate
    };
    this.setData(lastData);

  },
  moremovieTap: function (event) {
    var catetype = event.currentTarget.dataset.catetype;
    wx.navigateTo({
      url: './moremovie/moremovie?catetype=' + catetype,
      success: function (res) {
        // success
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  },
  onbindFocus: function (event) {
    var globalData = app.globalData;
    var search = event.detail.value;
    var searchurl = globalData.g_doubanapi + "/v2/movie/search?q=" + search;
    this.getMovielist(searchurl, "searchresult", '搜索结果');
    this.setData({
      containershow: false,
      searchpanelshow: true
    });

  },
  oncancleTap: function (event) {
    this.setData({
      containershow: true,
      searchpanelshow: false,
      searchresult:{}
    });

  },
  ondetailTap:function(event){

        var movieid=event.currentTarget.dataset.id;
        wx.navigateTo({
          url: 'moviedetail/moviedetail?id='+movieid,
          success: function(res){
            // success
          },
          fail: function(res) {
            // fail
          },
          complete: function(res) {
            // complete
          }
        })
      

  }
})